package com.lc.pianostudent.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2018/2/11.
 */

public class StringBufferUtil {

    public static String appendRedString (List<?> list) {

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i) + " ");
        }

        return sb.toString().trim();

    }

    /**
     * 组三复试组合拼接字符串
     * */
    public static String zuSanFuShiCombination (List<?> list) {

        String result = "";
        List<String> stringList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                String str1 = String.valueOf(list.get(i)) + String.valueOf(list.get(i)) + String.valueOf(list.get(j));
                String str2 = String.valueOf(list.get(j)) + String.valueOf(list.get(j)) + String.valueOf(list.get(i));
                String s = str1 + "," + str2;
                stringList.add(s);
            }
        }

        for (int i = 0; i < stringList.size(); i++) {
            result = result + stringList.get(i) + ",";
        }

        return result.substring(0, result.length() - 1);

    }

    /**
     * 计算阶乘数，即n! = n * (n-1) * ... * 2 * 1
     * @param n
     * @return
     */
    public static int factorial(int n) {
        return (n > 1) ? n * factorial(n - 1) : 1;
    }

    /**
     * 计算排列数，即A(n, m) = n!/(n-m)!
     * @param n
     * @param m
     * @return
     */
    public static int arrangement(int n, int m) {
        return (n >= m) ? factorial(n) / factorial(n - m) : 0;
    }

    /**
     * 计算组合数，即C(n, m) = n!/((n-m)! * m!)
     * @param n
     * @param m
     * @return
     */
    public static int combination(int n, int m) {
        return (n >= m) ? factorial(n) / factorial(n - m) / factorial(m) : 0;
    }

    /**
     * 随机选择
     * length 随机长度
     * size 随机个数
     * 从1开始
     * */
    public static int[] shuffle(int length, int size) {
        int[] shuffled = new int[length];
        int[] result = new int[size];
        Random rand = new Random();
        for (int i = 0; i < length; i++){
            shuffled[i] = i;
        }
        for (int i = 0; i < length; i++) {
            int tmp = rand.nextInt(length);//record the swaped place
            int tmp2 = shuffled[i];
            shuffled[i] = shuffled[tmp];
            shuffled[tmp] = tmp2;
        }
        for (int i = 0; i < size; i++) {
            result[i] = shuffled[i] + 1;//默认从0开始, 从1开始的话+1
        }

        return result;
    }

    /**
     * 随机选择
     * length 随机长度
     * size 随机个数
     * 从0开始
     * */
    public static int[] shuffleFromZero(int length, int size) {
        int[] shuffled = new int[length];
        int[] result = new int[size];
        Random rand = new Random();
        for (int i = 0; i < length; i++){
            shuffled[i] = i;
        }
        for (int i = 0; i < length; i++) {
            int tmp = rand.nextInt(length);//record the swaped place
            int tmp2 = shuffled[i];
            shuffled[i] = shuffled[tmp];
            shuffled[tmp] = tmp2;
        }
        for (int i = 0; i < size; i++) {
            result[i] = shuffled[i];//默认从0开始, 从1开始的话+1
        }

        return result;
    }

}
